package com.economy.swefunds;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class PortfolioActivity extends Activity implements PortfolioListener {
	
	private ListView PortfolioListView;
	private PortfolioList Portfolio;
	
	private ArrayList<String> NameList;
	private ArrayAdapter<String> fundsAdapter;

	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fund_overview);

        Portfolio = PortfolioList.getInstance(getApplicationContext());
        Portfolio.addObserver(this);

        NameList = new ArrayList<String>();
        
        fundsAdapter = new ArrayAdapter<String>(this, R.layout.fundlist, NameList);
        
        PortfolioListView = (ListView)findViewById(R.id.fundList);
		PortfolioListView.setTextFilterEnabled(true);
		PortfolioListView.setAdapter(fundsAdapter);
		
        updatePortfolio();

        
        Button watchButton = (Button)findViewById(R.id.watchbutton);
        watchButton.setOnClickListener(new View.OnClickListener()  {
			@Override
			public void onClick(View v) {
				// Empty
			}
		});
        
        PortfolioListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
			@Override
			public boolean onItemLongClick(AdapterView<?> a, View v, int pos, long id) {
				Object o = a.getAdapter().getItem(pos);
				String fund = o.toString();
				Portfolio.removeFundByName(fund);
				Toast.makeText(getApplicationContext(), "You have removed: " + fund, Toast.LENGTH_SHORT).show();
				return true;
			}
		});
    }

    public void updatePortfolio() {
    	NameList.clear();
        NameList.addAll(Portfolio.getNames());
    }

    @Override
	public void onPortfolioChanged() {
    	updatePortfolio();
	   	fundsAdapter.notifyDataSetChanged();
	}

}